package com.by.validate;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 验证
 */
public class V {
    /**
     * 处理hibernate验证结果
     *
     * @param result
     */
    public static void validate(BindingResult result) {     //获取验证错误信息
        List<FieldError> list = result.getFieldErrors();
        if (null != list && list.size() > 0) {
            Map map = new HashMap();
            //遍历list
            list.forEach(el -> {
                map.put(el.getField(), el.getDefaultMessage());
            });
            //抛出异常
            throw new ValidationException(map);
        }
    }
}
